<template>
	<view>
		<view class="nav">
			<view :class="navid == index?'navitem active':'navitem'" v-for="(item,index) in navlist" :key="index"
				@click="changeNav(index)">
				{{item}}
			</view>
		</view>
		<!-- 收货地址 -->
		<view class="addr" @tap="selectAddress" v-show="navid!=1">
			<view class="icon">
				<image src="/static/addricon.png" mode=""></image>
			</view>
			<view class="right" v-if="recinfo != ''">
				<view class="tel-name">
					<view class="name">
						{{recinfo.name}}
					</view>
					<view class="tel">
						{{recinfo.phone}}
					</view>
				</view>
				<view class="addres">
					{{recinfo.region}}
					{{recinfo.address}}
				</view>
			</view>
			<view class="right" v-else>
				请选择地址
			</view>
		</view>

		<!-- 购买商品列表 -->
		<view class="buy-list">
			<view class="row" v-for="(row,index) in buylist" :key="row.id">
				<view class="goods-info">
					<view class="img">
						<image :src="row.cover_path"></image>
					</view>
					<view class="info">
						<view class="title">{{row.name}}</view>
						<view class="spec">单价 : {{row.price}} ; 数量 : {{row.num}}</view>
						<view class="spec" v-if="row.mode == 2">
							配套数量 : {{row.support_num}}
						</view>
						<view class="price-number">
							<view class="price">￥{{row.amount}}</view>
							<view class="number">
								<!-- <view class="sub" @tap.stop="sub(index)">
									<view class="icon jian"></view>
								</view>
								<view class="input">
									<input type="number" :value="row.num" disabled="true" />
								</view>
								<view class="add" @tap.stop="add(index)">
									<view class="icon jia"></view>
								</view> -->
							</view>
						</view>
					</view>

				</view>
				<!-- 配套 -->
				<view class="buy-list" v-if="row.mode == 2">
					<view class="row" v-for="(support_goods,index2) in row.support" :key="support_goods.id">
						<view class="goods-info">
							<view class="img">
								<image :src="support_goods.goods.cover_path"></image>
							</view>
							<view class="info">
								<view class="title">{{support_goods.goods.name}}</view>
								<view class="spec">单价 : {{support_goods.goods.price}}</view>
								<view class="price-number">
									<!-- <view class="price">￥{{support_goods.amount}}</view> -->
									<view class="number">
										<view class="sub" @tap.stop="sub(support_goods)">
											<view class="icon jian"></view>
										</view>
										<view class="input">
											<input type="number" :value="support_goods.num" disabled="true" />
										</view>
										<view class="add" @tap.stop="add(support_goods)">
											<view class="icon jia"></view>
										</view>
									</view>
								</view>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<!-- 明细 -->
		<view class="detail">
			<view class="row">
				<view class="nominal">
					商品金额
				</view>
				<view class="content">
					￥{{goodsAmount|toFixed}}
				</view>
			</view>
			<view class="row">
				<view class="nominal">
					邮费
				</view>
				<view class="content">
					￥{{postage|toFixed}}
				</view>
			</view>
			<view class="row note">
				<view class="left">
					备注 :
				</view>
				<view class="right">
					<input placeholder="选填,备注内容" v-model="note" />
				</view>
			</view>
		</view>
		<view class="detail">
			<view class="title">
				<text></text>积分支付
			</view>
			<view class="radiobox">
				<checkbox-group name="" @change="changeCheck">
					<label v-for="(item,index) in payTypeList" :key="index">
						<text>{{item.text}}(￥{{item.balance}} )</text>
						<checkbox :value="String(item.value)" />
					</label>
				</checkbox-group>
			</view>
		</view>
		<view class="footer">
			<view class="settlement">
				<view class="sum">应付:<view class="money">￥{{goodsAmount|toFixed}}</view>
				</view>
				<button class="btn" :loading="loading" :disabled="loading" @tap="toPay">提交订单</button>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		buyNow,
		getOrderPreGoods,
		getOrderDefaultAddress
	} from "@/api/shop.js"
	export default {
		data() {
			return {
				navlist: ['配送'],
				navid: 0,
				buylist: [], //订单列表
				goodsAmount: 0.0, //商品合计价格
				// sumPrice: 0.0, //用户付款价格
				// freight: 0.00, //运费
				note: '', //备注
				int: 1200, //抵扣积分
				deduction: 0, //抵扣价格
				recinfo: [],
				vip_discount: 0,
				name: '',
				phone: '',
				goods_id: '',
				cart_ids: '',
				postage: '', //运费
				payTypeList: [],
				pay_type: [],
				loading: false
			};
		},
		onLoad(e) {
			if (e.goods_id) {
				this.goods_id = e.goods_id
			}
			if (e.cart_ids) {
				this.cart_ids = e.cart_ids
			}
			this.getOrderPreGoods()
			this.getOrderDefaultAddress()
		},
		onShow() {
			//页面显示时，加载订单信息

		},
		onBackPress() {
			//页面后退时候，清除订单信息
			// this.clearOrder();
		},
		filters: {
			toFixed: function(x) {
				return parseFloat(x).toFixed(2);
			}
		},
		methods: {
			changeCheck(e) {
				this.pay_type = e.detail.value
			},
			getOrderDefaultAddress() {
				getOrderDefaultAddress().then(res => {
					if (res.code == 0) {
						this.recinfo = res.data
					}
				})
			},
			getOrderPreGoods() {
				getOrderPreGoods({
					goods_id: this.goods_id,
					cart_ids: this.cart_ids
				}).then(res => {
					if (res.code == 0) {
						this.buylist = res.data.goodsData
						this.postage = res.data.postage
						this.goodsAmount = res.data.goodsAmount
						this.payTypeList = res.data.payType
					}
				})
			},

			add(item) {
				if (!item.allow_choose) {
					return uni.$showMsg(`数量只能选择${item.require_num}件`)
				}
				item.num++
				// this.sum()
			},
			sub(item) {
				if (!item.allow_choose) {
					return uni.$showMsg(`数量只能选择${item.require_num}件`)
				}
				if (item.num <= 0) {
					return;
				}
				item.num--;
				// this.sum()
			},
			sum() {
				this.goodsPrice = 0
				this.buylist.forEach(item => {
					if (item.yue_discount && item.yue_discount != 0) {
						let sum = item.num * item.goodsinfo.price
						this.goodsPrice = this.goodsPrice + (sum - sum * Number(item
							.goodsinfo.yue_discount) / 100);
					} else {
						this.goodsPrice = this.goodsPrice + (item.num * item.goodsinfo
							.price);
					}

				})
			},
			changeNav(e) {
				this.navid = e
			},
			toPay() {
				let that = this
				if (!this.recinfo.id) {
					uni.showToast({
						title: '请选择地址',
						icon: "none"
					})
					return
				}
				uni.showModal({
					title: '温馨提示',
					content: "是否同意7天鉴赏",
					confirmText: "同意",
					cancelText: "我要秒结",
					success(data) {
						let is_judge = 0;
						if (data.confirm) {
							is_judge = 1;
						}
						//本地模拟订单提交UI效果
						uni.showLoading({
							title: '正在提交订单...'
						})
						that.loading = true
						buyNow({
							address_id: that.recinfo.id || '',
							remarks: that.note,
							goodsData: that.buylist,
							pay_type: that.pay_type,
							is_judge: is_judge
						}).then(res => {
							setTimeout(() => {
								uni.hideLoading();
								uni.showToast({
									title: res.msg,
									icon: "none"
								})
								if (res.code == 0) {
									let data = res.data
									if (data.finishOrder) { //订单完成
										uni.redirectTo({
											url: '/pages/order/index'
										})
									} else {
										if (data.is_wx) {
											//微信支付  去授权当下微信环境
											let redirect_uri =
												`${that.$config.REDIRECT_URL}?amount=${that.goodsAmount}&order_id=${data.order_no}`
											if (data.user_openid) {
												uni.navigateTo({
													url: `/pages/orderpay/index?amount=${that.goodsAmount}&order_id=${data.order_no}`
												})
											} else {
												window.location =
													`https://open.weixin.qq.com/connect/oauth2/authorize?appid=${that.$config.WXAPPID}
												&redirect_uri=${encodeURIComponent(redirect_uri)}&response_type=code&scope=snsapi_userinfo&state=abcdefghijklmnopqrstuvwxyz#wechat_redirect`;
											}

										} else {
											uni.redirectTo({
												url: '/pages/order/index'
											})
										}
									}
								} else {
									that.loading = false
								}
							}, 500)
						})
					}
				})

			},
			//选择收货地址
			selectAddress() {
				uni.navigateTo({
					url: '/pages/address/address?type=select'
				})
			}
		}
	}
</script>

<style lang="scss">
	page {
		background: #f3f3f3;
		padding-top: 10px;
	}

	.timebox {
		display: flex;
		flex-wrap: wrap;
		width: 96%;
		margin: 0 auto;
		background: #fff;
		border-bottom-left-radius: 10px;
		border-bottom-right-radius: 10px;
		padding: 10px;
		padding-top: 0;
		box-sizing: border-box;

		.t-item {
			font-size: 12px;
			display: flex;
			flex-direction: column;
			align-items: center;
			width: 23%;
			margin: 5px 1%;
			justify-content: center;
			background-color: #eee;
			color: #000;
			height: 50px;
		}

		.active {
			background: #1d3f2b;
			color: #fff;
		}
	}


	.gdline {
		height: 3px;
		width: 50px;
		background-color: #f3f3f3;
		position: absolute;
		bottom: 8px;
		left: 50%;
		transform: translate(-50%);
		overflow: hidden;
	}

	.activeline {
		background-color: #1d3f2b;
		height: 100%;
		transition: ease .3s all;
	}



	.number {
		display: flex;
		justify-content: center;
		align-items: flex-end;

		.input {
			width: 100%;
			height: 45upx;
			margin: 0 10upx;

			input {
				width: 60upx;
				height: 45upx;
				display: flex;
				justify-content: center;
				align-items: center;
				text-align: center;
				font-size: 33upx;
			}
		}


		.sub,
		.add {
			width: 50upx;
			height: 50upx;
			border-radius: 50%;
			border: 1px solid #1d3f2b;
			display: flex;
			justify-content: center;
			align-items: center;

			.icon {
				font-size: 22upx;
				width: 50upx;
				height: 50upx;
				display: flex;
				justify-content: center;
				align-items: center;
				color: #1d3f2b;
			}
		}

		.add {
			background: #1d3f2b;
			color: #fff;

			.icon {
				color: #fff;
			}
		}
	}

	.self {
		width: 96%;
		margin: 0 auto;
		background: #fff;
		border-radius: 5px;
		padding: 10px;
		box-sizing: border-box;
		margin-top: 10px;

		.inputbox {
			display: flex;
			align-items: center;
			font-size: 14px;
			padding: 5px 0;

			text {
				width: 60px;
			}

			input {
				font-size: 14px;
				flex: 1;
				padding: 5px;
				background: #eee;
				border-radius: 4px;
			}
		}

		.selftips {
			display: flex;
			flex-direction: column;
			font-size: 12px;
			color: #999;
			margin-top: 5px;

			.tipsmsg {
				display: flex;
				align-items: center;
				justify-content: space-between;
				margin-top: 10px;

				.tipstext {
					display: block;
					width: 90%;
					overflow: hidden;
					text-overflow: ellipsis;
					white-space: nowrap;
				}
			}
		}
	}

	.nav {
		display: flex;
		align-items: center;
		border-bottom: 2px solid #1d3f2b;
		width: 96%;
		margin: 0 auto;
		background: #fff;
		border-top-left-radius: 5px;
		border-top-right-radius: 5px;

		.navitem {
			width: 28%;
			text-align: center;
			font-size: 14px;
			height: 40px;
			line-height: 40px;
		}

		.active {
			background: #1d3f2b;
			color: #fff;
			border-top-left-radius: 5px;
			border-top-right-radius: 5px;
		}
	}

	.addr {
		width: 90%;
		padding: 20upx 3%;
		margin: 20upx auto 0 auto;
		// box-shadow: 0upx 5upx 20upx rgba(0, 0, 0, 0.1);
		border-radius: 20upx;
		display: flex;
		background: #fff;

		.icon {
			width: 80upx;
			height: 80upx;
			display: flex;
			align-items: center;

			image {
				width: 60upx;
				height: 60upx;
			}
		}

		.tel-name {
			width: 100%;
			display: flex;
			font-size: 32upx;

			.tel {
				margin-left: 40upx;
			}
		}

		.addres {
			width: 100%;
			font-size: 26upx;
			color: #999;
		}
	}

	.buy-list {
		width: 90%;
		padding: 10upx 3%;
		margin: 20upx auto 20upx auto;
		// box-shadow: 0upx 5upx 20upx rgba(0, 0, 0, 0.1);
		border-radius: 20upx;
		background: #fff;

		.buy-title {
			font-weight: bold;
			padding: 10px 0;
		}

		.row {
			margin: 30upx 0;

			.goods-info {
				width: 100%;
				display: flex;

				.img {
					width: 90px;
					height: 80px;
					border-radius: 10upx;
					overflow: hidden;
					flex-shrink: 0;
					margin-right: 10upx;

					image {
						width: 80px;
						height: 80px;
					}
				}

				.info {
					width: 100%;
					height: 80px;
					overflow: hidden;
					display: flex;
					flex-wrap: wrap;
					position: relative;

					.title {
						width: 100%;
						font-size: 28upx;
						display: -webkit-box;
						-webkit-box-orient: vertical;
						-webkit-line-clamp: 2;
						// text-align: justify;
						overflow: hidden;
					}

					.spec {
						font-size: 22upx;
						background-color: #f3f3f3;
						color: #a7a7a7;
						height: 40upx;
						display: flex;
						align-items: center;
						padding: 0 10upx;
						border-radius: 20upx;
						margin-bottom: 20vw;
					}

					.price-number {
						position: absolute;
						width: 100%;
						bottom: 0upx;
						display: flex;
						justify-content: space-between;
						align-items: flex-end;
						font-size: 28upx;
						height: 40upx;
						padding-top: 5px;
						box-sizing: border-box;

						.price {
							color: #1d3f2b;
						}

						.number {
							display: flex;
							justify-content: center;
							align-items: center;

						}
					}
				}
			}
		}
	}

	.order {
		width: 90%;
		padding: 10upx 3%;
		margin: 20upx auto 20upx auto;
		// box-shadow: 0upx 5upx 20upx rgba(0, 0, 0, 0.1);
		border-radius: 20upx;
		background: #fff;

		.row {
			margin: 20upx 0;
			height: 40upx;
			display: flex;

			.left {
				font-size: 28upx;
			}

			.right {
				margin-left: 40upx;
				font-size: 26upx;
				color: #999;
				flex: 1;

				input {
					font-size: 26upx;
					color: #999;
				}
			}
		}
	}

	.blck {
		width: 100%;
		height: 100upx;
	}

	.footer {
		width: 100%;
		padding: 0 2%;
		background-color: #fbfbfb;
		height: 100upx;
		display: flex;
		justify-content: flex-end;
		align-items: center;
		font-size: 28upx;
		position: fixed;
		bottom: 0upx;
		z-index: 5;
		background: #fff;
		max-width: 500px;
		box-sizing: border-box;

		.settlement {
			width: 80%;
			display: flex;
			justify-content: flex-end;
			align-items: center;

			.sum {
				width: 50%;
				font-size: 28upx;
				margin-right: 10upx;
				display: flex;
				justify-content: flex-end;

				.money {
					font-weight: 600;
				}
			}

			.btn {
				padding: 0 30upx;
				height: 60upx;
				background-color: #1d3f2b;
				color: #fff;
				display: flex;
				justify-content: center;
				align-items: center;
				font-size: 30upx;
				border-radius: 40upx;
			}
		}
	}

	.detail {
		width: 90%;
		padding: 10upx 3%;
		margin: 20upx auto 20upx auto;
		// box-shadow: 0upx 5upx 20upx rgba(0, 0, 0, 0.1);
		border-radius: 20upx;
		background: #fff;

		.title {
			font-weight: bold;
			padding: 10px 0;
			font-size: 17px;
		}

		.radiobox {
			checkbox-group {
				width: 100%;
			}

			label {
				width: 100% !important;
				display: flex;
				justify-content: space-between;
				align-items: center;
				margin: 15px 0;
			}
		}

		.row {
			height: 60upx;
			display: flex;
			justify-content: space-between;
			align-items: center;

			.nominal {
				font-size: 28upx;
			}

			.content {
				font-size: 26upx;
				color: #1d3f2b;
				display: flex;
				align-items: center;
			}
		}

		.note {
			input {
				font-size: 14px;
				color: #999;
				text-align: right;
			}
		}
	}
</style>